using System;
using System.IO;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.Security;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for handler
/// </summary>
public class handler : IHttpHandler
{
    public handler()
    {
    }

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;
        switch (request.QueryString["status"])
        {
            case "page":
                if (request.QueryString["mode"] == "insert")
                    insertPage(context);
                else if (request.QueryString["mode"] == "update")
                    updatePage(context);
                else if (request.QueryString["mode"] == "delete")
                    deletePage(context);
                break;
            case "article":
                if (request.QueryString["mode"] == "insert")
                    insertArticle(context);
                else if (request.QueryString["mode"] == "update")
                    updateArticle(context);
                else if (request.QueryString["mode"] == "delete")
                    deleteArticle(context);
                else if (request.QueryString["mode"] == "load")
                    loadArticles();
                break;
            case "link":
                if (request.QueryString["mode"] == "insert")
                    insertLink(context);
                else if (request.QueryString["mode"] == "update")
                    updateLink(context);
                else if (request.QueryString["mode"] == "delete")
                    deleteLink(context);
                else if (request.QueryString["mode"] == "load")
                    loadLinks();
                break;
            case "images":
                loadImages(context);
                break;
            case "search":
                loadSearch(request.QueryString["criteria"],context);
                break;
            default:
                break;
        }
    }

    private void loadLinks()
    {
        BLL.Admin bll = new BLL.Admin();
        bll.getLinks();
    }

    private void deleteLink(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.deleteLink(request.Form["bookmarks_Id"]))
        {
            BLL.Admin bll = new BLL.Admin();
            bll.getLinks();
        }
    }

    private void updateLink(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.updateLink(request.Form["bookmarks_Id"],
                        request.Form["title"],
                        request.Form["url"],
                        request.Form["description"],
                        request.Form["target"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void insertLink(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.insertLink(request.Form["title"],
                        request.Form["url"],
                        request.Form["description"],
                        request.Form["target"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void loadSearch(string searchCriteria, HttpContext context)
    {
        BLL.Common bll = new BLL.Common();
        context.Response.Write(bll.searchData(searchCriteria));
    }
        
    private void loadImages(HttpContext context)
    {
        DirectoryInfo dirs=new DirectoryInfo(context.Server.MapPath("img/resources/"));
        FileInfo[] files=dirs.GetFiles();
        context.Response.Write("var tinyMCEImageList = new Array(");
        for(int i=0; i<files.Length; i++)
	    {
            FileInfo file=files[i];
            context.Response.Write("['" + file.Name + "','../img/resources/" + file.Name + "']");
            if(i!=files.Length-1)
                context.Response.Write(",");
	    }
        context.Response.Write(");");
	    // Name, URL
        //["Logo 1", "logo.jpg"],
        //["Logo 2 Over", "logo_over.jpg"]
    }

    private void loadArticles()
    {
        BLL.Admin bll = new BLL.Admin();
        bll.getArticles(HttpContext.Current.Request.Form["page_id"]);
    }

    private void insertArticle(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.insertArticle(request.Form["pages_id"],
                        request.Form["title"],
                        request.Form["content"],
                        request.Form["date_added"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void updateArticle(HttpContext context)
    {

        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.updateArticle(request.Form["article_Id"],
                        request.Form["pages_id"],
                        request.Form["title"],
                        request.Form["content"],
                        request.Form["date_added"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void deleteArticle(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.deleteArticle(request.Form["article_Id"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void insertPage(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.insertPage(request.Form["Has_Article"],
                        request.Form["Menu_title"],
                        request.Form["Title"],
                        request.Form["content"],
                        request.Form["date"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void updatePage(HttpContext context)
    {   
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.updatePage(request.Form["page_Id"],
                        request.Form["Has_Article"],
                        request.Form["Menu_title"],
                        request.Form["Title"],
                        request.Form["content"],
                        request.Form["date"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }

    private void deletePage(HttpContext context)
    {
        HttpRequest request = context.Request;
        DAL dal = new DAL();
        if (dal.deletePage(request.Form["page_id"]))
            context.Response.Write("Success...");
        else
            context.Response.Write("Failed...");
    }
}
